local jijiang = fk.CreateSkill {

  name = "joyex__jijiang$",

  tags = {  },

}



jijiang:addEffect("viewas", {
  name = "joyex__jijiang$",
  anim_type = "offensive",
  pattern = "slash",
  card_filter = Util.FalseFunc,
  view_as = function(self, player,cards)
    if #cards ~= 0 then
      return nil
    end
    local c = Fk:cloneCard("slash")
    c.skillName = jijiang.name
    return c
  end,
  before_use = function(self, player, use)
    local room = player.room
    if use.tos then
      room:doIndicate(player.id, data:getAllTargets())
    end
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if p.kingdom == "shu" then
        local cardResponded = room:askForResponse(p, "slash", "slash", "#joyex__jijiang-ask::" .. player.id, true)
        if cardResponded then
          room:responseCard({
            from = p,
            card = cardResponded,
            skipDrop = true,
          })
          use.card = cardResponded
          --room:setPlayerMark(p,"@@joyex__jijiang-phase",1)
          if not player.dead then player:drawCards(1,jijiang.name) end
          if not p.dead then p:drawCards(1,jijiang.name) end
          return
        end
      end
    end
    room:setPlayerMark(player, "jijiang-failed-phase", 1)
    return jijiang.name
  end,
  enabled_at_play = function(self, player)
    return player:getMark("jijiang-failed-phase") == 0 and not table.every(Fk:currentRoom().alive_players, function(p)
      return p == player or p.kingdom ~= "shu"
    end)
  end,
  enabled_at_response = function(self, player)
    return not table.every(Fk:currentRoom().alive_players, function(p)
      return p == player or p.kingdom ~= "shu"
    end)
  end,
})

jijiang:addEffect(fk.CardUsing, {
  name = "#joyex__jijiang_trigger",
  anim_type = "drawcard",
  events = {fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(jijiang.name) and target ~= player and target.kingdom == "shu" and data.card.trueName == "slash" and player.phase == player.NotActive and player:getMark("joyex__jijiangdraw-turn") == 0
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(target,jijiang.name,data,"#joyex__jijiangdraw::"..player.id) then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if not player.dead then
      room:setPlayerMark(player,"joyex__jijiangdraw-turn",1)
      player:drawCards(1,jijiang.name)
    end
  end,
})
jijiang:addEffect(fk.CardResponding, {
  name = "#joyex__jijiang_trigger",
  anim_type = "drawcard",
  events = {fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(jijiang.name) and target ~= player and target.kingdom == "shu" and data.card.trueName == "slash" and player.phase == player.NotActive and player:getMark("joyex__jijiangdraw-turn") == 0
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(target,jijiang.name,data,"#joyex__jijiangdraw::"..player.id) then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if not player.dead then
      room:setPlayerMark(player,"joyex__jijiangdraw-turn",1)
      player:drawCards(1,jijiang.name)
    end
  end,
})

return jijiang